﻿using Nest;
using PMS.Search.Domain.Entities;
using PMS.Search.Elasticsearch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ElasticSearchTest
{
    public class SchoolRankEsService
    {
        private readonly EsClient _client;
        private readonly EsIndexConfig.Index _index = EsIndexConfig.Instance.SchoolRankIndex;

        public SchoolRankEsService(EsClient client)
        {
            _client = client;
        }

        public CreateIndexResponse CreateIndex()
        {
            var resp = _client.GetClient().Indices.Delete(_index.Name);
            var createIndexResponse = _client.GetClient().Indices.Create(_index.Name, c => c
                .Aliases(ad => ad.Alias(_index.Alias))
                .Settings(s => s
                    .Setting("max_ngram_diff", 20)
                    .Setting("max_result_window", 500000)
                    .Analysis(AnalyzeBuilder.GetAnalysis())
                )
                .Map<SearchSchoolRank>(mm => mm
                .Dynamic(false)
                    .Properties(p => p
                        .Keyword(t => t
                            .Name(n => n.Id)
                        )
                        .Text(t => t
                            .Name(n => n.Title)
                            .Fields(f => f
                                .Text(ft
                                    => ft.Name("cntext")
                                    .Analyzer(AnalyzeBuilder.IkNgramAnalyzer)
                                    .SearchAnalyzer(AnalyzeBuilder.IkSearchAnalyzer))
                                .Text(ft
                                    => ft.Name("pinyin")
                                    .Analyzer(AnalyzeBuilder.IkPinyinAnalyzer)
                                    .SearchAnalyzer(AnalyzeBuilder.IkPinyinSearchAnalyzer))
                            )
                        )
                        .Nested<SchoolRankItem>(t => t
                            .Name(n => n.Schools)
                            .Properties(np => np
                                .Number(tt => tt.Name(tn => tn.RankNo))
                                .Text(tt => tt
                                    .Name(tn => tn.Name)
                                    .Fields(f => f
                                        .Text(ft
                                            => ft.Name("cntext")
                                            .Analyzer(AnalyzeBuilder.IkNgramAnalyzer)
                                            .SearchAnalyzer(AnalyzeBuilder.IkSearchAnalyzer))
                                        .Text(ft
                                            => ft.Name("pinyin")
                                            .Analyzer(AnalyzeBuilder.IkPinyinAnalyzer)
                                            .SearchAnalyzer(AnalyzeBuilder.IkPinyinSearchAnalyzer))
                            ))))
                        .Keyword(t=>t.Name(n=>n.CityCodes))
                        .Date(t => t.Name(n => n.UpdateTime))
                        .Boolean(t => t.Name(n => n.IsDeleted))
                        .Number(t => t.Name(n => n.Pv))
                        .Number(t => t.Name(n => n.Uv))
                    )
                )
            );
            return createIndexResponse;
        }
    }
}
